define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index' + location.search,
                    // add_url: 'goods/add',
                    // edit_url: 'goods/edit',
                    // del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    import_url: 'goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'store.name', title: __('商家名'), operate: 'LIKE'},
                        {field: 'store.phone', title: __('商家电话'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'content', title: __('Content'), operate: 'LIKE',
                            formatter : function(value, row, index, field){
                                if(value.length > 15){
                                    return "<span style='float:left;width:120px;display: block;overflow: hidden;text-overflow: ellipsis;white-space:nowrap;'>" + value +
                                        "</span><a style='float: left;font-size: 18px;' href='javascript:show(\"" + value +" \")'><i class='fa fa-angle-down'></i></a>";
                                }else{
                                    return value;
                                }
                            },
                            cellStyle : function(value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "150px"
                                    }
                                };
                            }},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: {0: __('待审核'), 1: __('通过'), 2: __('驳回')}},
                        {field: 'listing', title: __('上架'), formatter: Table.api.formatter.toggle, searchList: {0: __('下架'), 1: __('上架')}},

                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate',  buttons: [{
                                name: '审核',
                                hidden:function(row){
                                    if(row.status != 0){
                                        return true;
                                    }
                                },
                                text: __('审核'),
                                classname: 'btn btn-danger btn-xs btn-detail btn-dialog',
                                url: function(row){
                                    return 'goods/edit/ids/'+row.id;
                                }
                            }], title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

function show(text){
    layer.open({
        title:false,
        btn:false,
        content:text
    })
}